package com.kwq.shixi.controller;

import com.kwq.shixi.model.ExcelSheetPO;
import com.kwq.shixi.model.ExportData;
import com.kwq.shixi.model.Wz;
import com.kwq.shixi.util.EntityUtils;
import com.kwq.shixi.util.ExcelUtil;
import com.kwq.shixi.util.ExportExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Api
public class ExcelController {
    /**
     * 导出
     *
     * @param
     * @return
     * @throws Exception
     */

    @GetMapping(value = "/exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        //伪造list数据
        List<Wz> list = new ArrayList<>();
        list.add(new Wz());
        list.add(new Wz());
        list.add(new Wz());
        list.add(new Wz());
        list.add(new Wz());
        list.add(new Wz());
        list.add(new Wz());
        list.add(new Wz());
        list.add(new Wz());

        if (list != null && list.size() > 0) {
            ExportData data = new ExportData();
            String[] titles = new String[]{"w12131", "w2", "w3", "w4", "w5", "w6", "w7", "w8", "w9"};
            //设置表格头
            data.setTitles(titles);

            // 格式化数据
            List<String[]> rowList = changeDate(list);
            data.setList(rowList);

            String fileName = "封装excel导出功能测试.xlsx";
            data.setFileName(fileName);
            ExportExcelUtil.exportExcel(response, data);
        } else {
            ExportExcelUtil.nullResponse(response, 2);
        }
    }

    private List<String[]> changeDate(List<Wz> list) {
        List<String[]> rowList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            for (Wz ld : list) {
                String[] colArr = new String[9];
                //伪造日期
                colArr[0] = sdf.format(new Date());
                //伪造数据
                colArr[1] = ld.getW1();
                colArr[2] = ld.getW2();
                colArr[3] = ld.getW3();
                colArr[4] = ld.getW4();
                colArr[5] = ld.getW5();
                colArr[6] = ld.getW6();
                colArr[7] = ld.getW7();
                colArr[8] = ld.getW8();
                rowList.add(colArr);
            }

        }

        return rowList;
    }

    /*
     * 导入功能
     * */
    @ApiOperation(value = "导入需求导入用户", notes = "导入需求导入用户")
    @PostMapping(value = "/importDemandUser")
    public void importDemandUser(@RequestParam(value = "file") MultipartFile file) throws IOException {
        importDemandUser1(file);
        return;
    }

    public void importDemandUser1(MultipartFile file) throws IOException {
        List<ExcelSheetPO> esList = ExcelUtil.readExcelOnLine(file, null, null);
        if (esList != null && esList.size() > 0) {
            //默认只取第一个sheet的数据
            ExcelSheetPO es = esList.get(0);
            List<List<Object>> dataList = es.getDataList();

            if (dataList != null) {
                for (int i = 0; i < dataList.size(); i++) {
                    List<Object> ol = dataList.get(i);
                    if (ol != null && ol.size() > 0) {
                        Wz du = new Wz();
                        du.setW1("w1");

                        String userName = (String) ol.get(1);
                        if (!StringUtils.isEmpty(userName)) {
                            du.setW2(userName);
                        }

                        String city = (String) ol.get(3);
                        if (!StringUtils.isEmpty(city)) {
                            du.setW3(city);
                        }

                    }
                   /* String isControl = (String) ol.get(4);
                    if(!StringUtils.isEmpty(isControl)){
                        if("是".equals(isControl.trim())){
                            du.setIsControl(1);
                        }else{
                            du.setIsControl(0);
                        }
                    }

                    String minPeakCutPower =  (String) ol.get(5);
                    if(!StringUtils.isEmpty(minPeakCutPower)){
                        du.setMinPeakCutPower(Double.parseDouble(minPeakCutPower));
                    }

                    String maxPeakCutPower =  (String) ol.get(6);
                    if(!StringUtils.isEmpty(maxPeakCutPower)){
                        du.setMaxPeakCutPower(Double.parseDouble(maxPeakCutPower));
                    }

                    String minPeakCutDuration =  (String) ol.get(7);
                    if(!StringUtils.isEmpty(minPeakCutDuration)){
                        du.setMinPeakCutDuration(minPeakCutDuration);
                    }

                    String maxPeakCutDuration =  (String) ol.get(8);
                    if(!StringUtils.isEmpty(maxPeakCutDuration)){
                        du.setMaxPeakCutDuration(maxPeakCutDuration);
                    }

                    String baseLoad =  (String) ol.get(9);
                    if(!StringUtils.isEmpty(baseLoad)){
                        if("√".equals(baseLoad.trim())){
                            du.setBaseLoad(1);
                        }else{
                            du.setBaseLoad(0);
                        }
                    }*/

                    //进行持久化操作
                }

            }
        }

    }
}
